//
//  QSetUpPassWordVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/21.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QSetUpPassWordVC.h"
#import "QSetUpPassWordView.h"
#import "QPayMoneyVC.h"
@interface QSetUpPassWordVC ()<UIScrollViewDelegate>

@property(nonatomic,strong)QSetUpPassWordView *topTextView;
@property(nonatomic,strong)QSetUpPassWordView *bottomTextView;


@end

@implementation QSetUpPassWordVC{

    NSString *_onePassWordStr;
    NSString *_twoPassWordStr;
    
    UIScrollView *_scrollView;
    
    UIButton *_agreeButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置密码";
    [self setupForDismissKeyboard];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,(kNavHeight+kStatusHeight),kScreenWidth, kScreenHeight-(kNavHeight+kStatusHeight))];
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenHeight-(kNavHeight+kStatusHeight));
    _scrollView.scrollEnabled = NO;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];

    [self createTopView];
    [self createBottomView];
    
}

//第一页
- (void)createTopView{

    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,kScreenHeight-(kNavHeight+kStatusHeight))];
    topView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [_scrollView addSubview:topView];
    
    UILabel *topLabel = [QViewUtility createLabelWithFrame:CGRectMake((kScreenWidth-200)/2, 50, 200, 18) font:16 textAlignment:NSTextAlignmentCenter textColor:[QColorUtility defaultBackColor] text:@"请输入密码"];
    [topView addSubview:topLabel];
    
    _topTextView = [[QSetUpPassWordView alloc] initWithFrame:CGRectMake((kScreenWidth-250)/2, topLabel.bottom+15, 250, 55)];
    _topTextView.count = 4;
    __weak QSetUpPassWordVC *wself = self;
    _topTextView.passwordBlock = ^(NSString *str){
        __strong QSetUpPassWordVC *sSelf = wself;
        sSelf->_onePassWordStr = str;
        CLog(@"_onePassWordStr=====%@",sSelf->_onePassWordStr);
        if(sSelf->_onePassWordStr.length == 4){
            [sSelf performSelector:@selector(nextAction) withObject:sSelf afterDelay:0.8];
        }
    };
    [topView addSubview:_topTextView];
    
    UILabel *bottomLabel = [QViewUtility createLabelWithFrame:CGRectMake((kScreenWidth-250)/2, _topTextView.bottom+15, 250, 18) font:14 textAlignment:NSTextAlignmentCenter textColor:[QColorUtility defaultDeepGrayColor] text:@"此密码用于余额支付,请谨慎设置"];
    [topView addSubview:bottomLabel];
}

//第二页
- (void)createBottomView{

    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth,0, kScreenWidth,kScreenHeight-(kNavHeight+kStatusHeight))];
    bottomView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [_scrollView addSubview:bottomView];
    
    UILabel *topLabel = [QViewUtility createLabelWithFrame:CGRectMake((kScreenWidth-200)/2, 50, 200, 18) font:16 textAlignment:NSTextAlignmentCenter textColor:[QColorUtility defaultBackColor] text:@"请再次输入密码"];
    [bottomView addSubview:topLabel];
    
     _bottomTextView = [[QSetUpPassWordView alloc] initWithFrame:CGRectMake((kScreenWidth-250)/2, topLabel.bottom+15, 250, 55)];
    _bottomTextView.count = 4;
    __weak QSetUpPassWordVC *wself = self;
    _bottomTextView.passwordBlock = ^(NSString *str){
        __strong QSetUpPassWordVC *sSelf = wself;
       sSelf->_twoPassWordStr = str;
        CLog(@"_twoPassWordStr=====%@",sSelf->_twoPassWordStr);
        
    };
    [bottomView addSubview:_bottomTextView];
    
    UIButton *bacButton = [QViewUtility createButtonWithFrame:CGRectMake(0, _bottomTextView.bottom+15, kScreenWidth, 30) font:16 title:@"返回上一级" titleColor:[QColorUtility defaultBlueColor] backgroundColor:[UIColor clearColor]];
    bacButton.tag = 1;
    [bacButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:bacButton];

    
    _agreeButton = [QViewUtility createButtonWithFrame:CGRectMake(0, kScreenHeight-50-(kNavHeight+kStatusHeight), kScreenWidth, 50) font:17 title:@"完成" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [_agreeButton blueStyle];
    _agreeButton.tag = 2;
    [_agreeButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:_agreeButton];
}

- (void)btnAction:(UIButton *)btn{
    if(btn.tag == 1){//返回上一级
        _twoPassWordStr = nil;
        [_bottomTextView clearText];
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

    }else{//完成
        if(![_onePassWordStr isEqualToString:_twoPassWordStr]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"密码不一致" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }else{
            [self requestSetupPassWord];
        }
    }
}

- (void)nextAction{
    [_topTextView clearText];
    [_scrollView setContentOffset:CGPointMake(kScreenWidth,0) animated:YES];
}


- (void)requestSetupPassWord{

    NSString *url = [QParamService urlWithAction:@"setPaymentPassword"];
    
    NSString *md5Str = [QMD5Hash md5String:_twoPassWordStr];
    md5Str = [md5Str uppercaseString];
    NSDictionary *param = [QParamService setPassWord:md5Str];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"设置成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            [QUserInfo sharedInfo].loginModel.isPayPassword = @"1";
            [QDataUtility updateLoginModel:[QUserInfo sharedInfo].loginModel];
            
            for(UIViewController *vc in self.navigationController.viewControllers){
                if([vc isKindOfClass:[QPayMoneyVC class]]){
                    [self.navigationController popToViewController:vc animated:YES];
                }
            }
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

@end
